package cn.bearspark.tag.provider.dao.buffer;

import cn.bearspark.framework.redis.starter.buffer.list.RedisBaseListBuffer;
import cn.bearspark.tag.provider.dao.key.TagBufferKeyBuilder;
import cn.bearspark.tag.provider.dao.mapper.TagBaseMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 最近最热的 9 个标签 id 的缓存器
 * <p>
 * 本缓存器无需 clean，等待 5min 后自然清理即可
 *
 * @author f
 */
@Component
public class RecentTop9TagIdBuffer extends RedisBaseListBuffer<Long> {

    private final TagBaseMapper tagBaseMapper;

    private final TagBufferKeyBuilder tagBufferKeyBuilder;

    public RecentTop9TagIdBuffer(RedisTemplate<String, Object> redisTemplate,
                                 TagBaseMapper tagBaseMapper,
                                 TagBufferKeyBuilder tagBufferKeyBuilder) {
        super(redisTemplate);
        this.tagBaseMapper = tagBaseMapper;
        this.tagBufferKeyBuilder = tagBufferKeyBuilder;
    }

    /**
     * 查询最近最热的 9 个标签 id
     *
     * @return 最近最热的 9 个标签 id
     */
    public List<Long> listRT9TagId() {
        return super.range(
                tagBufferKeyBuilder.buildRT9TagKey(),
                0, 9,
                tagBaseMapper::listRecentTop9TagId
        );
    }
}
